package com.feng.annotation.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.feng.annotation.property.DataSourceProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author Lee
 * @date 2019/3/6
 */
@Configuration
@PropertySource(value = {"classpath:db.properties"})
public class DaoConfig {

    @Bean
    public DataSource dataSource(DataSourceProperty dataSourceProperty){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(dataSourceProperty.getUsername());
        dataSource.setPassword(dataSourceProperty.getPassword());
        dataSource.setUrl(dataSourceProperty.getUrl());
        dataSource.setDriverClassName(dataSourceProperty.getDriverClass());
        return dataSource;
    }

   /* @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);

        sqlSessionFactory.setTypeAliasesPackage("com.feng.annotation.bean");

        //mapper配置
        Resource[] resources = new Resource[]{};
        sqlSessionFactory.setMapperLocations(resources);

        Properties configurationProperties = new Properties();
        configurationProperties.setProperty("lazyLoadingEnabled","true");
        configurationProperties.setProperty("aggressiveLazyLoading","true");
        configurationProperties.setProperty("cacheEnabled","true");
        configurationProperties.setProperty("defaultExecutorType","SIMPLE");
        configurationProperties.setProperty("mapUnderscoreToCamelCase","true");

        //分页插件配置
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = configurationProperties;
        properties.setProperty("helperDialect","mysql");
        pageInterceptor.setProperties(properties);
        sqlSessionFactory.setPlugins(new Interceptor[]{pageInterceptor});

        return sqlSessionFactory;
    }*/

    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /*@Bean
    public SqlSessionTemplate sqlSession(SqlSessionFactoryBean sqlSessionFactory){
        try {
            return new SqlSessionTemplate(sqlSessionFactory.getObject(), ExecutorType.BATCH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(SqlSessionFactoryBean sqlSessionFactory) {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.feng.annotation.dao");
        return mapperScannerConfigurer;
    }*/

}
